package com.kun.filter;

import com.kun.entity.User;
import com.kun.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        //过滤器 从Session中获取用户
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);

        if(user == null){//用户处于下线状态
            resp.sendRedirect(req.getContextPath()+"/error.jsp");
        }else {
            chain.doFilter(request,response);
        }

    }

    public void destroy() {

    }
}
